/* ObjectUtils.java

 {{IS_NOTE
 Purpose:
 
 Description:
 
 History:
 2007-07-30, Created by wangfeifei
 }}IS_NOTE

 Copyright (C) 2007 wangfeifei919@126.com. All Rights Reserved.

 {{IS_RIGHT
 This program is distributed under GPL Version 2.0 in the hope that
 it will be useful, but WITHOUT ANY WARRANTY.
 }}IS_RIGHT
 */
package com.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

/**
 * 
 * @author wangfeifei
 * 
 */
public class ObjectUtils
{
	private static final Log log = LogFactory.getLog(ObjectUtils.class);
	private static String datePattern = "yyyy-MM-dd";
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	public static Date toDate(String s, boolean flag)
	{
		try
		{
			if ((s == null ) || (s.length() < 1 ))
			{
				if (!flag)
				{
					return null;
				}
				else
				{
					return new java.sql.Date(System.currentTimeMillis());
				}
			}
			if (s.indexOf(" ") > 0)
			{
				s = s.substring(0, s.indexOf(" "));
			}
			return java.sql.Date.valueOf(s);
		}
		catch (Exception e)
		{
			if (!flag)
			{
				return null;
			}
			else
			{
				return new java.sql.Date(System.currentTimeMillis());
			}
		}
	}
	public static Date toDate(Date d, boolean flag)
	{
		if (flag && d == null)
		{
			return new java.sql.Date(System.currentTimeMillis());
		}
		else
		{
			return java.sql.Date.valueOf(dateFormat.format(d));
		}
	}
	public static Date toDateTime(Date d, boolean flag)
	{
		if (flag && d == null)
		{
			return new java.sql.Date(System.currentTimeMillis());
		}
		else
		{
			return java.sql.Date.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ).format(d));
		}
	}
	public static Date toDate(Date d)
	{
		if (d == null)
		{
			// return java.sql.Date.valueOf("1900-1-1");
			return null;
		}
		return java.sql.Date.valueOf(dateFormat.format(d));
	}
	public static String toDateString(Date d)
	{
		if ((d == null ) || (String.valueOf(d).length() < 1 ))
		{
			// return toString(java.sql.Date.valueOf("1900-1-1"));
			return null;
		}
		String s = toString(d);
		if (s.indexOf(" ") > 0)
		{
			s = s.substring(0, s.indexOf(" "));
		}
		return toString(java.sql.Date.valueOf(s));
	}
	public static Integer toInteger(Object s)
	{
		if ((s == null ) || (String.valueOf(s).length() < 1 ))
		{
			return Integer.valueOf(0);
		}
		return Integer.valueOf(String.valueOf(s));
	}
	public static Float toFloat(Object s)
	{
		if ((s == null ) || (String.valueOf(s).length() < 1 ))
		{
			return Float.valueOf("0.00");
		}
		return Float.valueOf(String.valueOf(s));
	}
	public static Double toDouble(Object s)
	{
		if ((s == null ) || (String.valueOf(s).length() < 1 ))
		{
			return Double.valueOf("0.00");
		}
		return Double.valueOf(String.valueOf(s));
	}
	public static Long toLong(Object s)
	{
		try
		{
			String code = String.valueOf(s).replace(" ", "");
			if (s == null || code.length() < 1)
			{
				return Long.valueOf("0");
			}
			return Long.valueOf(code);
		}
		catch (Exception e)
		{
			return 0l;
		}
	}
	public static String toString(Object s)
	{
		if ((s == null ) || (String.valueOf(s).length() < 1 ))
		{
			return null;
		}
		return String.valueOf(s);
	}
	public static HSSFRichTextString toHSSFRichTextString(Object s)
	{
		if ((s == null ) || (String.valueOf(s).length() < 1 ))
		{
			return null;
		}
		return new HSSFRichTextString(String.valueOf(s));
	}
	public static HSSFRow getRowFromSheetByRow(HSSFSheet sheet, int row)
	{
		HSSFRow hRow = sheet.getRow(row);
		if (hRow == null)
		{
			hRow = sheet.createRow(row);
		}
		return hRow;
	}
	@SuppressWarnings( { "deprecation" })
	public static HSSFCell getCellFromSheetByCell(HSSFRow row, int cell)
	{
		HSSFCell hCell = row.getCell((short) cell);
		if (hCell == null)
		{
			hCell = row.createCell((short) cell);
		}
		hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		return hCell;
	}
	@SuppressWarnings( { "deprecation" })
	public static HSSFCell getCellFromSheetByCell(HSSFRow row, int cell, boolean flag)
	{
		HSSFCell hCell = null;
		if (flag)
		{
			hCell = row.createCell((short) cell);
		}
		else
		{
			hCell = row.getCell((short) cell);
			if (hCell == null)
			{
				hCell = row.createCell((short) cell);
			}
		}
		hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		return hCell;
	}
	public static BigDecimal toBigDecimal(Object s)
	{
		if (s == null || String.valueOf(s).toLowerCase().equals("null"))
		{
			return BigDecimal.valueOf(0.00);
		}
		return BigDecimal.valueOf(Double.valueOf(String.valueOf(s)));
	}
}
